﻿using System;
using System.Globalization;
using System.Web.Routing;
using System.Web.Mvc;
using Fuse8.ContentManagementFramework.Administration.Attributes;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.SystemFacade.Globalization;
using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.Web.ApplicationRuntime;
using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    [AdminCurrentCulture]
    public abstract class CmsBaseController : CmsBaseApplicationController
    {
        #region Properties

        public IFormsAuthenticationService FormsService { get; protected set; }

        public IMembershipService MembershipService { get; protected set; }

        public CmsSiteService SiteService { get; protected set; }

        public CmsUser ContextUser
        {
            get
            {
                return CmsContext.ContextUser;
            }
        }

        protected ICmsContext CmsContext
        {
            get
            {
                return DependencyServiceLocator.Current.GetInstance<ICmsContext>();
            }
        }

        public bool IsInAdministrationArea
        {
            get
            {
                //TO DO
                return false;
            }
        }

        #endregion

        #region Initialize()

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null)
            {
                if (!DependencyServiceLocator.Current.IsDefaultServiceRegistered<IFormsAuthenticationService>())
                {
                    FormsService = new FormsAuthenticationService();

                    DependencyServiceLocator.Current.SetDefaultService(FormsService);
                }
                else
                {
                    FormsService = DependencyServiceLocator.Current.GetInstance<IFormsAuthenticationService>();
                }
            }

            if (MembershipService == null)
            {
                if (!DependencyServiceLocator.Current.IsDefaultServiceRegistered<IMembershipService>())
                {
                    MembershipService = new CmsMembershipService();

                    DependencyServiceLocator.Current.SetDefaultService(MembershipService);
                }
                else
                {
                    MembershipService = DependencyServiceLocator.Current.GetInstance<IMembershipService>();
                }
            }

            if (SiteService == null)
            {
                SiteService = new CmsSiteService();
            }

            base.Initialize(requestContext);
        }

        #endregion

        #region View

        public ActionResult UserHasNoRights()
        {
            return View();
        }

        #endregion

        #region 404

        protected override void HandleUnknownAction(string actionName)
        {
            var action = Prepare404View();

            action.ExecuteResult(ControllerContext);
        }

        #endregion

        protected static string GetText(string original, string domain, params object[] values)
        {
            return GlobalizationFacade.Current.WithDomain(domain, CultureInfo.CurrentUICulture)
                .GetText(original, values);
        }
    }
}